/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package editor;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

import map.MemMap;

import figures.Figure;
import figures.basic.ImageFigure;
import figures.basic.PoliLine;
import figures.cartography.Avenue;
import figures.cartography.Bloq;
import figures.cartography.Building;
import figures.cartography.Limit;
import figures.cartography.River;
import figures.cartography.SpeedWay;
import figures.cartography.Square;
import figures.cartography.Street;
import figures.cartography.Train;
import figures.cartography.Water;
import figures.images.Bar;
import figures.images.GasStation;
import figures.images.PostOffice;
import figures.images.Restaurant;
import figures.images.SubwayStation;
import figures.images.WebIcon;

public class NameDialog extends JFrame {
	private static final long serialVersionUID = 1L;

	public NameDialog(final Figure selectedFigure, final MemMap m, final ActionListener listener) {
		final TextField t = new TextField();
		final JComboBox c = new JComboBox();
		final TextField n_image = new TextField();
		c.addItem(Street.class);
		c.addItem(Avenue.class);
		c.addItem(Bloq.class);
		c.addItem(Building.class);
		c.addItem(Limit.class);
		c.addItem(River.class);
		c.addItem(SpeedWay.class);
		c.addItem(Square.class);
		c.addItem(Train.class);
		c.addItem(Water.class);
		c.addItem(SubwayStation.class);
		c.addItem(PostOffice.class);
		c.addItem(Bar.class);
		c.addItem(Restaurant.class);
		c.addItem(GasStation.class);
		c.addItem(WebIcon.class);
		c.setSelectedItem(selectedFigure.getClass());
		Button b = new Button("Set Name");
		if(selectedFigure instanceof WebIcon){
			n_image.setText(Integer.toString(((WebIcon)selectedFigure).getN()));
		}
		t.setText(new String(selectedFigure.getName()));
		getContentPane().add(t, BorderLayout.NORTH);
		getContentPane().add(c, BorderLayout.CENTER);
		getContentPane().add(b, BorderLayout.SOUTH);
		getContentPane().add(n_image, BorderLayout.EAST);
		b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				selectedFigure.setName(t.getText());
				int[] xx = null;
				int[] yy = null;
				if(selectedFigure instanceof PoliLine){
					xx = ((PoliLine)selectedFigure).getx();
					yy = ((PoliLine)selectedFigure).gety();
				}
				if(selectedFigure instanceof ImageFigure){
					xx = new int[1];
					yy = new int[1];
					xx[0] = ((ImageFigure)selectedFigure).xcenter();
					yy[0] = ((ImageFigure)selectedFigure).ycenter();
				}
				if(c.getSelectedItem()!=selectedFigure.getClass()){
					try {
						Figure ff = (Figure)((Class)c.getSelectedItem()).newInstance();
						if(ff instanceof PoliLine){
							PoliLine nuevo = (PoliLine) ff;
							nuevo.setId(selectedFigure.getId());
							nuevo.setName(new String(selectedFigure.getName()));
							nuevo.setxy(xx, yy);
							m.removeFigure(selectedFigure);
							m.appendFigure(nuevo);
						}
						if(ff instanceof WebIcon){
							((WebIcon)ff).setN(Integer.parseInt(n_image.getText()));
						}
						if(ff instanceof ImageFigure){
							ImageFigure n = (ImageFigure) ff;
							n.setName(new String(selectedFigure.getName()));
							n.setPos(xx[0],yy[0]);
							//n.setName(selectedFigure.getName());
							m.removeFigure(selectedFigure);
							m.appendFigure(n);
						}
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				dispose();
				listener.actionPerformed(null);
			}});
		pack();
		setVisible(true);
	}
	
}
